package com.thanh.icook;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.AdapterView.OnItemClickListener;

public class ListDishActivity extends Activity{
	public static String iddish;
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.layout_listdish);
        getlistDish();
    } 

    public void getlistDish(){   	
	 final GridviewAdapter_ListDish mAdapter;
   	 ArrayList<String> ListDishText;
   	 ArrayList<Bitmap> ListDishImage;
   	 ArrayList<String> ListDishDescription = new ArrayList<String>();
   	 ArrayList<String> ListDishEnergy = new ArrayList<String>();
   	 final ArrayList<String> ListID = new ArrayList<String>();
   	 GridView gV_Catedish;
   	 Creadfile read = new Creadfile();
     String jsonCatedish = read.readJsonURL("http://icook.dinhchithanh.com/getdish.php?id="+QuanLyActivity.idcate+"&min=0&max=10");
     ListDishText = new ArrayList<String>();
     ListDishImage = new ArrayList<Bitmap>();
	 JSONArray entries;
		try {
			entries = new JSONArray(jsonCatedish);
			for(int i=0;i<entries.length();i++){
				JSONObject post = entries.getJSONObject(i);
				ListDishText.add(post.getString("title"));
				//ID.add(post.getString("id"));
				Bitmap img = read.DownloadImage(post.getString("urlimage"));
				ListDishImage.add(img);
				ListDishDescription.add(post.getString("description"));
				ListDishEnergy.add(post.getString("energy"));
				ListID.add(post.getString("id"));
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
       
       // prepared arraylist and passed it to the Adapter class
       //mAdapter = new GridviewAdapter_ListDish(this,ListDishText, ListDishImage,);
       mAdapter = new GridviewAdapter_ListDish(this, ListDishText, ListDishImage, 
       		ListDishDescription, ListDishEnergy);
       
       // Set custom adapter to gridview
       gV_Catedish = (GridView) findViewById(R.id.gridView_listdish);
       gV_Catedish.setAdapter(mAdapter);
       // Implement On Item click listener
       gV_Catedish.setOnItemClickListener(new OnItemClickListener() 
       {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				iddish = ListID.get(position);
				//Toast.makeText(ListDishActivity.this, mAdapter.getItem(position), Toast.LENGTH_SHORT).show();
				startActivity(new Intent(ListDishActivity.this, DetailDishActivity.class));
			}
		});
     
    }
}
